//
//  TabMainViewController.swift
//  shop
//
//  Created by 刘伟 on 2017/2/25.
//
//

import UIKit

class TabMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 创建 子视图
        let home = HomeController()
        let history = HistoryController()
        //let find = FindController()
        let me = MeController()
        
        // 实例 子视图
        let homeNV = addChildVC(childVC: home, title: "当前资产", image: "tabbar_home", selectedImage: "tabbar_home_highlighted")
        let historyNV = addChildVC(childVC: history, title: "归档资产", image: "tabbar_category", selectedImage: "tabbar_category_highlighted")
        //let findNV = addChildVC(childVC: find, title: "发现", image: "tabbar_cart", selectedImage: "tabbar_cart_highlighted")
        let meNV = addChildVC(childVC: me, title: "我的", image: "tabbar_me", selectedImage: "tabbar_me_highlighted")
        
        // 配置 装填 子视图
        let tabHeight = self.tabBar.frame.height
        homeNV.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - tabHeight)
        historyNV.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - tabHeight)
        //findNV.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - tabHeight)
        meNV.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - tabHeight)
        
        // 添加 子视图
        self.addChildViewController(homeNV)
        self.addChildViewController(historyNV)
        //self.addChildViewController(findNV)
        self.addChildViewController(meNV)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if Defaults[.needUpdate] == true {
            let confirm = confirmDialog(title: "温馨提示", message: "轻账本(LiteAccount) 有新版啦，快去更新吧。", sure: {
                toAppStore()
            })
            self.present(confirm, animated: true, completion: nil)
            Defaults[.needUpdate] = false
        }
    }
    
    // 封装的，设置 tabBarItem 属性的方法：返回值：UINavigationController
    func addChildVC(childVC: UIViewController, title: String, image: String, selectedImage: String) -> UINavigationController {
        // navigationBar 中的文字
        childVC.navigationItem.title = title
        // tabBar 中的图标跟文字
        childVC.tabBarItem.image = UIImage(named: image)
        childVC.tabBarItem.selectedImage = UIImage(named: selectedImage)
        childVC.tabBarItem.title = title
        // 设置子视图
        let navigationC = UINavigationController(rootViewController: childVC)
        return navigationC
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
